package activity.order;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.text.DecimalFormat;
import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import presenter.SysTemPresenter;
import util.EdiTextPut;
import util.utls.ByAlert;
import util.utls.IsUtilUid;
import util.volley.ResultEntity;

/**
 * 报价
 */
public class ActivityChangeThePrice extends BaseActivity implements Observer, TextWatcher {

    private TextView tvTitle;
    SysTemPresenter presenter;
    private EditText edtWeiXiuPrice, edtCaiLiaoPrice;
    private TextView tvHejiPrice;
    double dble, top, bwn;
    String id, url;
    private Button btnOrder;
    ImageView back;
    String cai,jin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actiivty_change_the_price);
        EdiTextPut.setPricePoint(edtWeiXiuPrice);
        EdiTextPut.setPricePoint(edtCaiLiaoPrice);

        id = getIntent().getStringExtra("id");
        url = getIntent().getStringExtra("url");
        tvTitle.setText("报价");
        Log.d("werwerwerwe", id);
        btnOrder.setText("报价");
        presenter = new SysTemPresenter(this);
        edtWeiXiuPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    top = Double.valueOf(edtWeiXiuPrice.getText().toString()+"");
                    DecimalFormat df = new DecimalFormat("#0.00");
                    dble = top + bwn;
                    tvHejiPrice.setText("¥" + df.format(dble));

                } catch (Exception ex) {
                    top=0;
                    dble = top + bwn;
                    DecimalFormat df = new DecimalFormat("#0.00");
                    tvHejiPrice.setText("¥" + df.format(dble));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edtCaiLiaoPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    bwn = Double.valueOf(edtCaiLiaoPrice.getText().toString()+"");
                    DecimalFormat df = new DecimalFormat("#0.00");
                    dble = top + bwn;
                    tvHejiPrice.setText("¥" + df.format(dble));

                } catch (Exception ex) {
                    bwn=0;
                    dble = top + bwn;
                    DecimalFormat df = new DecimalFormat("#0.00");
                    tvHejiPrice.setText("¥" + df.format(dble));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tvHejiPrice.setText("¥0.00");
        btnOrder = (Button) findViewById(R.id.btnOrder);
        cai=edtCaiLiaoPrice.getText().toString();
        jin=edtWeiXiuPrice.getText().toString();
        btnOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (edtWeiXiuPrice.getText().toString().equals("")&&edtCaiLiaoPrice.getText().toString().equals("")) {
                    ByAlert.alert("请输入金额");
                } else{
                    String  sbs=edtWeiXiuPrice.getText().toString();
                    String  strsb=edtCaiLiaoPrice.getText().toString();
                    double ble=0;
                    double cailiaos=0;
                    if(!sbs.equals("")){
                        ble=Double.parseDouble(sbs)*100;
                    }
                    if(!strsb.equals("")){
                        cailiaos=Double.parseDouble(strsb)*100;
                    }

                    showProgressDialog("请稍后...",false);
                    presenter.onsetPrice(IsUtilUid.isUid(), id, ble ,cailiaos);
                }
            }
        });

    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.SetPrice_success) {
                ByAlert.alert(entity.getData()+ "");
//                Intent intent1 = new Intent(ActivityChangeThePrice.this, WeiXiuOkActivity.class);
//                intent1.putExtra("title", url);
//                startActivity(intent1);
                finish();
            }
            if (entity.getEventType() == presenter.SetPrice_fail) {

                ByAlert.alert(entity.getData() + "");
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {



    }
}
